package com.eedi.framework.job.dal.mapper;

import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.job.controller.org.vo.log.OrgJobLogPageReq;
import com.eedi.framework.job.dal.dataobject.OrgJobLogDO;
import com.eedi.framework.mybatis.core.mapper.BaseMapperX;
import com.eedi.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;

/**
 * 任务日志 Mapper
 *
 * @author liuyf
 */
@Mapper
public interface OrgJobLogMapper extends BaseMapperX<OrgJobLogDO> {

    default PageResult<OrgJobLogDO> selectPage(OrgJobLogPageReq reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrgJobLogDO>()
                .eqIfPresent(OrgJobLogDO::getOrgJobId, reqVO.getOrgJobId())
                .likeIfPresent(OrgJobLogDO::getHandlerName, reqVO.getHandlerName())
                .geIfPresent(OrgJobLogDO::getBeginTime, reqVO.getBeginTime())
                .leIfPresent(OrgJobLogDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(OrgJobLogDO::getStatus, reqVO.getStatus())
                // ID 倒序
                .orderByDesc(OrgJobLogDO::getId)
        );
    }
    /**
     * 物理删除指定时间之前的日志
     *
     * @param createTime 最大时间
     * @param limit 删除条数，防止一次删除太多
     * @return 删除条数
     */
    @Delete("DELETE FROM org_job_log WHERE create_time < #{createTime} LIMIT #{limit}")
    Integer deleteByCreateTimeLt(@Param("createTime") LocalDateTime createTime, @Param("limit") Integer limit);
}
